<?php

/*
 * Built by :
 * Andi Kurniawan (fandikurniawan89@gmail.com) at system analis
 */

class Admin extends CI_Controller {

    function __construct() {
        parent::__construct();
        /* Proses autentikasi login dan hak akses */
        //start
        $login = $this->session->userdata('login');
        $privilege = $this->session->userdata('pri');
        if ($login != TRUE || $privilege != 1) {
            redirect('main/logout');
        }
        //end
    }

    function index() {
        redirect('admin/pengajuan'); //halaman index di redirect ke home.
    }

    function home() {
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/home');
        $this->load->view('mhs/footer');
    }

    //tampil semua data pengajuan judul
    function pengajuan() {
        if ($this->mmr->pengajuan_judul()) {
            $judul['judul'] = $this->mmr->pengajuan_judul();
        } else {
            $judul['judul'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/judul', $judul);
        $this->load->view('mhs/footer');
    }

    /* Saran */

    function saran() {
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/saran');
        $this->load->view('mhs/footer');
    }

    /* Insert saran ke dalam database */

    function save_saran() {
        $input = array(
            'judul_id' => $this->input->post('id'),
            'saran' => $this->input->post('saran'),
            'from' => $this->session->userdata('username')
        );
        $this->mmr->simpan_saran($input);
        $id = $this->input->post('id');
        $update = array(
            'status' => 'Revision'
        );
        $this->mmr->update_judul_saran($id, $update);
        /* em */
        $get_nim = $this->mmr->get_nim_judul($id);
        $nim = $get_nim->nim;
        if ($this->mmr->email_mhs($nim)) {
            $da = $this->mmr->email_mhs($nim);
        } else {
            $da = NULL;
        }
        $email = $da->email;
        $this->load->library('email');
        $this->email->clear();
        $this->email->to($email);
        $this->email->from('noreply@andikurniawan.com');
        $this->email->subject('Pendaftaran judul proposal.');
        $this->email->message('Judul anda telah di revisi, silahkan cek di web. Terima kasih.');
        $this->email->send();
        /* End em */
        redirect('admin/pengajuan');
    }

    /* End proses saran */

    /* Proses persetujuan */

    function confirm() {
        $id = $this->uri->segment(3);
        if ($this->mmr->confirm($id)) {
            $data['judul'] = $this->mmr->confirm($id);
        } else {
            $data['judul'] = array();
        }
        if ($this->mmr->dosen_bimb()) {
            $data['dosen'] = $this->mmr->dosen_bimb();
        } else {
            $data['dosen'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/confirm', $data);
        $this->load->view('mhs/footer');
    }

    /* Proses persetujuan judul */

    function acc() {
        $id = $this->input->post('id');
        $data = array(
            'status' => 'Approved'
        );
        $this->mmr->acc($id, $data);
        $dosen = array(
            'id' => $id,
            'dosen_1' => $this->input->post('dos'),
            'dosen_2' => $this->input->post('dosen'),
            'judul' => $this->input->post('judul')
        );
        $this->mmr->pembimbing($dosen);
        /* em */
        $get_nim = $this->mmr->get_nim_judul($id);
        $nim = $get_nim->nim;
        if ($this->mmr->email_mhs($nim)) {
            $da = $this->mmr->email_mhs($nim);
        } else {
            $da = NULL;
        }
        $aa = array(
            'nip' => $this->input->post('dos'),
            'nim' => $nim
        );
        $aba = array(
            'nip' => $this->input->post('dosen'),
            'nim' => $nim
        );
        $this->mmr->nip_nim($aa);
        $this->mmr->nip_nim($aba);
        $email = $da->email;
        $this->load->library('email');
        $this->email->clear();
        $this->email->to($email);
        $this->email->from('noreply@andikurniawan.com');
        $this->email->subject('Pendaftaran judul proposal.');
        $this->email->message('Judul anda telah di setujui, silahkan cek di web. Terima kasih.');
        $this->email->send();
        $do = $this->input->post('dos');
        if ($this->mmr->get_email_dosen($do)) {
            $e = $this->mmr->get_email_dosen($do);
        } else {
            $e = NULL;
        }
        $em = $e->email;
        $this->email->clear();
        $this->email->to($em);
        $this->email->from('noreply@andikurniawan.com');
        $this->email->subject('Bimbingan tesis.');
        $this->email->message('Anda mendapat tugas membimbing, silahkan cek di web. Terima kasih.');
        $this->email->send();
        $dos = $this->input->post('dosen');
        $ema = $this->mmr->get_email_dosen($dos);
        if ($this->mmr->get_email_dosen($dos)) {
            $ema = $this->mmr->get_email_dosen($dos);
        } else {
            $ema = NULL;
        }
        $mail = $ema->email;
        $this->email->clear();
        $this->email->to($mail);
        $this->email->from('noreply@andikurniawan.com');
        $this->email->subject('Bimbingan tesis.');
        $this->email->message('Anda mendapat tugas membimbing, silahkan cek di web. Terima kasih.');
        $this->email->send();
        //*end em*/
        redirect('admin/pengajuan');
    }

    /* End Proses persetujuan */

    /* Proses menampilkan judul yang telah di ACC */
    /* Start */

    function daftar_judul() {
        if ($this->mmr->daftar_judul()) {
            $judul['judul'] = $this->mmr->daftar_judul();
        } else {
            $judul['judul'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/daftarjudul', $judul);
        $this->load->view('mhs/footer');
    }

    /* End */

    /* Jadwal proposal start */

    function jadwal_proposal() {
        if ($this->mmr->jdwl_proposal()) {
            $judul['jadwal'] = $this->mmr->jdwl_proposal();
        } else {
            $judul['jadwal'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/jdwl_proposal', $judul);
        $this->load->view('mhs/footer');
    }

    function detail_proposal() {
        $id = $this->uri->segment(3);
        if ($this->mmr->det_pro($id)) {
            $data['seminar'] = $this->mmr->det_pro($id);
        } else {
            $data['seminar'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/detail_proposal', $data);
        $this->load->view('mhs/footer');
    }

    function proposal() {
        $id = $this->uri->segment(3);
        $n = $this->mmr->get_nim_seminar($id);
        $data['nim'] = $n->nim;
        if ($this->mmr->dosen()) {
            $data['dosen'] = $this->mmr->dosen();
        } else {
            $data['dosen'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/proposal', $data);
        $this->load->view('mhs/footer');
    }

    function proposal_save() {
        $id = $this->input->post('id');
        $data = array(
            'tgl' => $this->input->post('date'),
            'dosen_1' => $this->input->post('dosen_1'),
            'dosen_2' => $this->input->post('dosen_2'),
            'dosen_3' => $this->input->post('dosen_3'),
            'dosen_4' => $this->input->post('dosen_4'),
        );
        $this->mmr->jdwl_proposal_save($id, $data);
        redirect('admin/jadwal_proposal');
    }

    /* End Jadwal proposal */

    /* Jadwal proposal start */

    function jadwal_seminar() {
        if ($this->mmr->jdwl_seminar()) {
            $judul['jadwal'] = $this->mmr->jdwl_seminar();
        } else {
            $judul['jadwal'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/jdwl_seminar', $judul);
        $this->load->view('mhs/footer');
    }

    function detail_seminar() {
        $id = $this->uri->segment(3);
        if ($this->mmr->det_sem($id)) {
            $data['seminar'] = $this->mmr->det_sem($id);
        } else {
            $data['seminar'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/detail_seminar', $data);
        $this->load->view('mhs/footer');
    }

    function hasil_seminar() {
        $id = $this->uri->segment(3);
        if ($this->mmr->det_hasil_sem($id)) {
            $data['seminar'] = $this->mmr->det_hasil_sem($id);
        } else {
            $data['seminar'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/detail_hasil_seminar', $data);
        $this->load->view('mhs/footer');
    }

    function seminar() {
        $id = $this->uri->segment(3);
        $n = $this->mmr->get_nim_seminar($id);
        $data['nim'] = $n->nim;
        if ($this->mmr->dosen()) {
            $data['dosen'] = $this->mmr->dosen();
        } else {
            $data['dosen'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/seminar', $data);
        $this->load->view('mhs/footer');
    }

    function seminar_hasil() {
        $id = $this->uri->segment(3);
        $n = $this->mmr->get_nim_seminar($id);
        $data['nim'] = $n->nim;
        if ($this->mmr->dosen()) {
            $data['dosen'] = $this->mmr->dosen();
        } else {
            $data['dosen'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/seminar_hasil', $data);
        $this->load->view('mhs/footer');
    }

    function seminar_save() {
        $id = $this->input->post('id');
        $data = array(
            'tgl' => $this->input->post('date'),
            'dosen_1' => $this->input->post('dosen_1'),
            'dosen_2' => $this->input->post('dosen_2'),
            'dosen_3' => $this->input->post('dosen_3'),
            'dosen_4' => $this->input->post('dosen_4'),
        );
        $this->mmr->jdwl_proposal_save($id, $data);
        $n = $this->mmr->get_nim_seminar($id);
        $nim = $n->nim;
        $dea = array(
            'stat' => 'Deactive'
        );
        $this->mmr->deactive($nim, $dea);
        redirect('admin/jadwal_seminar');
    }

    function seminar_hasil_save() {
        $id = $this->input->post('id');
        $data = array(
            'tgl' => $this->input->post('date'),
            'dosen_1' => $this->input->post('dosen_1'),
            'dosen_2' => $this->input->post('dosen_2'),
            'dosen_3' => $this->input->post('dosen_3'),
            'dosen_4' => $this->input->post('dosen_4'),
        );
        $this->mmr->jdwl_hasil_save($id, $data);
        redirect('admin/jadwal_hasil');
    }

    /* End Jadwal proposal */
    /* Jadwal proposal start */

    function jadwal_hasil() {
        if ($this->mmr->jdwl_hasil()) {
            $judul['jadwal'] = $this->mmr->jdwl_hasil();
        } else {
            $judul['jadwal'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/hasil_seminar', $judul);
        $this->load->view('mhs/footer');
    }

    function detail_hasil() {
        $id = $this->uri->segment(3);
        if ($this->mmr->det_sem($id)) {
            $data['seminar'] = $this->mmr->det_sem($id);
        } else {
            $data['seminar'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/detail_seminar', $data);
        $this->load->view('mhs/footer');
    }

    function hasil() {
        $id = $this->uri->segment(3);
        $n = $this->mmr->get_nim_seminar($id);
        $data['nim'] = $n->nim;
        if ($this->mmr->dosen()) {
            $data['dosen'] = $this->mmr->dosen();
        } else {
            $data['dosen'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/seminar', $data);
        $this->load->view('mhs/footer');
    }

    function hasil_save() {
        $id = $this->input->post('id');
        $data = array(
            'tgl' => $this->input->post('date'),
            'dosen_1' => $this->input->post('dosen_1'),
            'dosen_2' => $this->input->post('dosen_2'),
            'dosen_3' => $this->input->post('dosen_3'),
            'dosen_4' => $this->input->post('dosen_4'),
        );
        $this->mmr->jdwl_proposal_save($id, $data);
        $n = $this->mmr->get_nim_seminar($id);
        $nim = $n->nim;
        $dea = array(
            'stat' => 'Deactive'
        );
        $this->mmr->deactive($nim, $dea);
        redirect('admin/jadwal_seminar');
    }

    /* End Jadwal proposal */

    /* Report */

    function rep_judul() {
        $this->load->library('pdf');
        if ($this->mmr->daftar_pengajuan_judul()) {
            $judul['judul'] = $this->mmr->daftar_pengajuan_judul();
        } else {
            $judul['judul'] = array();
        }
//        $this->load->view('report/rep_judul', $judul);
        $this->pdf->load_view('report/rep_judul', $judul);
        $this->pdf->render();
        $this->pdf->stream("Daftar Pengajuan Judul Tesis.pdf");
    }
    function rep_acc_judul() {
        $this->load->library('pdf');
        if ($this->mmr->daftar_judul()) {
            $judul['judul'] = $this->mmr->daftar_judul();
        } else {
            $judul['judul'] = array();
        }
        $this->pdf->load_view('report/rep_acc_jud', $judul);
        $this->pdf->render();
        $this->pdf->stream("Daftar Judul Tesis ACC.pdf");
    }

    function rep_sch_pro() {
        $this->load->library('pdf');
        if ($this->mmr->rep_jdwl_proposal()) {
            $judul['isi'] = $this->mmr->rep_jdwl_proposal();
        } else {
            $judul['isi'] = array();
        }
        $this->pdf->load_view('report/rep_sch_pro', $judul);
        $this->pdf->render();
        $this->pdf->stream("Daftar Jadwal Seminar Proposal Tesis.pdf");
    }

    function rep_sch_tes() {
        $this->load->library('pdf');
        if ($this->mmr->rep_jdwl_proposal()) {
            $judul['isi'] = $this->mmr->rep_jdwl_tesis();
        } else {
            $judul['isi'] = array();
        }
        $this->pdf->load_view('report/rep_sch_tesis', $judul);
        $this->pdf->render();
        $this->pdf->stream("Daftar Jadwal Seminar Tesis.pdf");
    }

    /* End report */

    function info() {
        if ($this->mmr->info()) {
            $data['info'] = $this->mmr->info();
        } else {
            $data['info'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/info', $data);
        $this->load->view('mhs/footer');
    }

    /* Surat */

    function surat() {
        if ($this->mmr->srt()) {
            $data['surat'] = $this->mmr->srt();
        } else {
            $data['surat'] = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/surat', $data);
        $this->load->view('mhs/footer');
    }

    function letter() {
        $this->load->library('parser');
        $id = $this->uri->segment(3);
        if ($this->mmr->ltr($id)) {
            $surat = $this->mmr->ltr($id);
        } else {
            $surat = array();
        }
        $data['id'] = $surat->id;
        $data['no'] = $surat->no;
        $data['lokasi'] = $surat->lokasi;
        $data['nama'] = $surat->nama;
        $data['nim'] = $surat->nim;
        $data['alamat'] = $surat->alamat;
        $data['judul'] = $surat->judul;
        $data['dos1'] = $surat->nados_1;
        $data['dos2'] = $surat->nados_2;
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/letter', $data);
        $this->load->view('mhs/footer');
    }

    function get_letter() {
        $this->load->library('parser');
        $id = $this->uri->segment(3);
        if ($this->mmr->ltr($id)) {
            $data['surat'] = $this->mmr->ltr($id);
        } else {
            $data = array();
        }
        $nama = $data['surat']->nim . '.pdf';
        $this->load->library('pdf');
        $this->pdf->load_view('admin/srt', $data);
        $this->pdf->render();
        $this->pdf->stream($nama);
        $upd = array('cetak' => 'Sudah');
        $this->mmr->cetak($id, $upd);
    }

    /* End Surat */

    /* Dosen */

    function dosen() {
        $this->load->library('pagination');
        $config['base_url'] = $this->config->config['base_url'] . '/index.php/admin/dosen/';
        $config['total_rows'] = $this->mmr->count_dosen();
        $config['per_page'] = 10;
        $config['num_links'] = 2;
        $config['uri'] = $this->uri->segment(3);
        $this->pagination->initialize($config);
        if ($this->mmr->get_dosen($config)) {
            $data['dosen'] = $this->mmr->get_dosen($config);
        } else {
            $data = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/dosen', $data);
        $this->load->view('mhs/footer');
    }

    function dosen_add() {
        $input = array(
            'username' => $this->input->post('nip'),
            'password' => $this->encrypt->sha1($this->input->post('nip')),
            'pri' => 2
        );
        $this->mmr->user_add($input);
        $insert = array(
            'nip' => $this->input->post('nip'),
            'nama' => $this->input->post('nama'),
            'email' => $this->input->post('email'),
            'telp' => $this->input->post('telp'),
        );
        $this->mmr->insert_dosen($insert);
        redirect('admin/dosen');
    }

    function mahasiswa() {
        $this->load->library('pagination');
        $config['base_url'] = $this->config->config['base_url'] . '/index.php/admin/mahasiswa/';
        $config['total_rows'] = $this->mmr->count_mahasiswa();
        $config['per_page'] = 10;
        $config['num_links'] = 2;
        $config['uri'] = $this->uri->segment(3);
        $this->pagination->initialize($config);
        if ($this->mmr->mahasiswa($config)) {
            $data['dosen'] = $this->mmr->mahasiswa($config);
        } else {
            $data = array();
        }
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/mahasiswa', $data);
        $this->load->view('mhs/footer');
    }

    function editMahasiswa() {
        $id = $this->uri->segment(3);
        $mhs = $this->mmr->get_mahasiswa($id);
        $data = array(
            'nim' => $mhs->nim,
            'nama' => $mhs->nama,
            'telp' => $mhs->telp,
            'email' => $mhs->email,
            'alamat' => $mhs->alamat,
            'tahun' => $mhs->angkatan,
        );
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->parser->parse('admin/editMahasiswa', $data);
        $this->load->view('mhs/footer');
    }

    function mahasiswa_add() {
        $input = array(
            'username' => $this->input->post('nim'),
            'password' => $this->encrypt->sha1($this->input->post('nim')),
            'pri' => 3
        );
        $this->mmr->user_add($input);
        $insert = array(
            'nim' => $this->input->post('nim'),
            'nama' => $this->input->post('nama'),
            'telp' => $this->input->post('telp'),
            'email' => $this->input->post('email'),
            'alamat' => $this->input->post('alamat'),
            'angkatan' => $this->input->post('tahun')
        );
        $this->mmr->insert_mahasiswa($insert);
        redirect('admin/mahasiswa');
    }

    function mahasiswa_update() {
        $id = $this->input->post('nim');
        $update = array(
            'nama' => $this->input->post('nama'),
            'telp' => $this->input->post('telp'),
            'email' => $this->input->post('email'),
            'alamat' => $this->input->post('alamat'),
            'angkatan' => $this->input->post('tahun')
        );
        $this->mmr->updateMahasiswa($id, $update);
        redirect('admin/mahasiswa');
    }

    function password() {
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/password');
        $this->load->view('mhs/footer');
    }

    function passNew() {
        $id = $this->session->userdata('username');
        $update = array('password' => $this->encrypt->sha1($this->input->post('pass')));
        $this->mmr->updPassAdm($id, $update);
        redirect('admin/password');
    }

    function gantiPassword() {
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/gantiPass');
        $this->load->view('mhs/footer');
    }

    function gantiPass() {
        $id = $this->uri->segment(3);
        $data = array('password' => $this->encrypt->sha1($id));
//        $id = $this->input->post('id');
//        $data = array('password' => $this->encrypt->sha1($this->input->post('pas')));
        $this->mmr->gantiPas($id, $data);
        redirect('admin/success');
    }

    function success() {
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/success');
        $this->load->view('mhs/footer');
    }

    function del() {
        $this->load->view('admin/header');
        $this->load->view('admin/nav-bar');
        $this->load->view('admin/del');
        $this->load->view('mhs/footer');
    }

    function delUser() {
        $id = $this->uri->segment(3);
        $this->mmr->delUser($id);
        redirect('admin/del');
    }

    function setActive() {
        $id = $this->uri->segment(3);
        $active = array('status' => 'Active');
        $this->mmr->infoStat($id, $active);
        redirect('admin/info');
    }

    function setDeactive() {
        $id = $this->uri->segment(3);
        $active = array('status' => 'Deactive');
        $this->mmr->infoStat($id, $active);
        redirect('admin/info');
    }

}

?>
